﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Adex;
using System.Transactions;
using Telerik.Web.UI;
using MediaMonitor.Common;

namespace MediaMonitor.Admin
{
    public partial class editRole : Adex.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Utility.AddDateControlToPage(this.Page);
                FillCompanies();
                string uid = Utility.GetRequestedVal("uid", "0");
                string cid = Utility.GetRequestedVal("cid", "0");
                if (cid != "0")
                {
                    hdnCurCId.Value = cid;
                    Utility.SelectListItem(lstCompany, cid);
                }
                if (uid == "0")
                {
                    hdnFormMode.Value = "add";
                    hdnCurPkId.Value = uid;
                }
                else
                {
                    hdnFormMode.Value = "edit";
                    hdnCurPkId.Value = uid;
                    DoEdit();
                }
            }
            lblMsg.Text = lblMsg_top.Text = "";
        }
        private void FillCompanies()
        {
            lstCompany.DataSourceID = "sqlDSComp";
            lstCompany.DataBind();
            lstCompany.Items.Insert(0, new ListItem("- Select a Role -", "0"));
        }
        protected void DoEdit()
        {
            VSiteUser usr = new VSiteUser("SiteUserID", hdnCurPkId.Value);
            Utility.SelectListItem(lstCompany, usr.CompanyCode.ToString());
            if (usr.SiteUserID > 0)
            {
                tbLoginName.Text = usr.UserName;
                tbLoginName.Enabled = false; // username can't be changed
                hdnCurCId.Value = usr.UserID.ToString();
            }
            pnlForm.Focus();
        }
        protected void btnUpdate_Click(object sender, ImageClickEventArgs e)
        {
            if (!IsValid) { return; }
            if (hdnFormMode.Value == "edit" && hdnCurPkId.Value != "0")
            {
                //company selection check.
                if (lstCompany.SelectedItem == null)
                {
                    lblMsg.Text = lblMsg_top.Text = "You need to select a valid Role from drop down list";
                    return;
                }
                SPs.Updaterole(hdnCurCId.Value, lstCompany.SelectedValue).Execute();
                Response.Redirect("ChangeRole.aspx");
            }
        }
        protected void btnDelete_Click(object sender, ImageClickEventArgs e)
        {
            if (hdnCurPkId.Value.Length > 0 && hdnFormMode.Value == "edit")
            {
                try
                {
                    SiteUser usr = new SiteUser(hdnCurPkId.Value);
                    AppUser.DeleteSiteUser(usr);
                    SiteUser.Delete(hdnCurPkId.Value);
                    lblMsg.Text = lblMsg_top.Text = "Selected User deleted successfully";
                }
                catch (Exception ex)
                {
                    lblMsg.Text = lblMsg_top.Text = "Selected User cannot be deleted. [" + ex.Message + "]";
                }
                hdnFormMode.Value = "add";
                hdnCurPkId.Value = "0";
                Utility.ClearForm(pnlForm);
                pnlIno.Visible = true;
                pnlForm.Visible = false;
            }
        }
        protected void btnCancel_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("ChangeRole.aspx");
            pnlIno.Visible = true;
            pnlForm.Visible = false;
        }
    }
}